package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.Result;
import cn.itcast.pojo.CheckGroup;
import cn.itcast.pojo.Setmeal;
import cn.itcast.service.CheckGroupService;
import cn.itcast.service.SetmealService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequestMapping("/setmeal")
@RestController
public class SetmealHtmlController {

    @Reference
    private SetmealService setmealService;

    @Reference
    private CheckGroupService checkGroupService;


    /**
     * 套餐列表展示:
     * 查询Setmeal所有信息
     * @return
     */
    @GetMapping("/findAll")
    public Result findAll(){
        List<Setmeal> setmeal = setmealService.findAll();
        return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS,setmeal);
    }


    /**
     * 套餐详情展示
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result userFindAll(@PathVariable(value = "id")Integer id) {
        //根据ID查询到Setmeal详情
        Setmeal setmeal = setmealService.findById(id);

        //根据ID得到所有对应的t_checkgroup对象
        List<CheckGroup> checkGroups = checkGroupService.findBySetmealIdWithDetails(id);

        //将对象封装进Setmeal对象
        setmeal.setCheckGroups(checkGroups);

        //返回对象
        return new Result(true,null,setmeal);
    }
}
